﻿using System;
using System.Collections.Generic;
using System.Text;
using Newtonsoft.Json.Linq;
using System.Reflection;
using System.Collections;
using XLua;
using System.IO;
using MySql.Data.MySqlClient;

namespace ETModel
{
    [LuaCallCSharp]
    public class Mysql
    {
        public static MySqlConnection conn2 = null;

        public static MySqlConnection GetMySqlConnection()
        {
            MySqlConnection conn = null;
            if (null == conn || conn.State == System.Data.ConnectionState.Closed || conn.State == System.Data.ConnectionState.Broken)
            {
                try
                {
                    string ip = "127.0.0.1";
                    string port = "1107";
                    string user = "root";
                    string password = "root";
                    string databasename = "satdb";
                    string connetStr = $"server={ip};port={port};user={user};password={password};database={databasename};Charset=utf8;";

                    conn = null;
                    conn = new MySqlConnection(connetStr);
                    conn.Open();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    conn = null;
                }
            }
            return conn;
        }

        public static MySqlConnection GetMySqlConnection2()
        {
            if (null == conn2 || conn2.State == System.Data.ConnectionState.Closed || conn2.State == System.Data.ConnectionState.Broken)
            {
                try
                {
                    string ip = "127.0.0.1";
                    string port = "1107";
                    string user = "root";
                    string password = "root";
                    string databasename = "satdb";
                    string connetStr = $"server={ip};port={port};user={user};password={password};database={databasename};Charset=utf8;";

                    conn2 = null;
                    conn2 = new MySqlConnection(connetStr);
                    conn2.Open();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    conn2 = null;
                }
            }
            return conn2;
        }

        public static MySqlConnection GetMySqlConnection3()
        {
            MySqlConnection conn3 = null;
            if (null == conn3 || conn3.State == System.Data.ConnectionState.Closed || conn3.State == System.Data.ConnectionState.Broken)
            {
                try
                {
                    string ip = "127.0.0.1";
                    string port = "3306";
                    string user = "root";
                    string password = "666666";
                    string databasename = "transfer";
                    string connetStr = $"server={ip};port={port};user={user};password={password};database={databasename};Charset=utf8;";

                    conn3 = null;
                    conn3 = new MySqlConnection(connetStr);
                    conn3.Open();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    conn3 = null;
                }
            }
            return conn3;
        }

        public static bool ExecuteSql(String sql)
        {
            MySqlConnection conn = GetMySqlConnection3();
            try
            {
                using (MySqlCommand command = new MySqlCommand(sql, conn))
                {
                    int result = command.ExecuteNonQuery();
                    // result = 1 则 返回真
                    return result != 0;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught: {0}", e);
                return false;
            }
            finally
            {
                conn.Dispose();
            }
        }

        public static MySqlDataReader Select(String sql, MySqlConnection conn)
        {
            try
            {
                if (conn == null || sql == "") return null;
                MySqlCommand command = new MySqlCommand(sql, conn);
                MySqlDataReader data = command.ExecuteReader();
                return data;
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception caught: {0}", e);
                return null;
            }
        }


        /*
         * 使用mysql
                MySqlConnection conn = Mysql.GetMySqlConnection();
                string sql = "";
                sql = "select fbalance,fsat,ferc,fstatus from t_addressmap where ferc='"+address+"'";
                using (MySqlDataReader dt = Mysql.Select(sql, conn))
                {
                    if (dt == null) return "error";
                    if (dt.Read())
                    {
                        ResInfo info = new ResInfo();
                        info.dict = new Dictionary<string, string>();
                        info.dict.Add("balance",dt["fbalance"].ToString());
                        info.dict.Add("sat", dt["fsat"].ToString());
                        info.dict.Add("erc", dt["ferc"].ToString());
                        info.dict.Add("fstatus", dt["fstatus"].ToString());
                        return JsonHelper.ToJson(info);
                    }
                }
         */

    }
}
